package com.briup.cms.service.impl;

import com.briup.cms.bean.BaseUser;
import com.briup.cms.constant.CmsConstants;
import com.briup.cms.dao.BaseUserMapper;
import com.briup.cms.dao.extend.BaseUserExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseUserService;
import com.briup.cms.utils.JwtUtil;
import com.briup.cms.utils.MessageStatus;
import com.briup.cms.vm.UpdateUserVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 *
 * 用户服务层
 */
@Service
public class BaseUserServiceImpl implements IBaseUserService {
	@Autowired
	private BaseUserExtendMapper baseUserExtendMapper;
	@Autowired
	private BaseUserMapper baseUserMapper;

	@Override
	public String login(String username, String password) throws CustomerException {
		BaseUser user = baseUserExtendMapper.findUserByName(username);

		if(user==null){
			throw new CustomerException(MessageStatus.USER_NOT_EXIST);
		}

		if(user.getStatus() != CmsConstants.USER_STATUS_NORMAL){
			throw new CustomerException(MessageStatus.USER_ACCOUNT_FORBIDDEN);
		}

		if(user.getRole() != CmsConstants.SYSTEM_ROLE_ADMIN){
			throw new CustomerException(MessageStatus.PERMISSION_NOT_ADMIN);
		}

		if(!user.getPassword().equals(password)){
			throw new CustomerException(MessageStatus.USER_LOGIN_ERROR);
		}

		Map<String,Object> map = new HashMap<>();
		map.put("username",user.getUsername());
		map.put("realname",user.getRealname());
		String token = JwtUtil.sign(user.getUserId().toString(),map);

		return token;

	}

	@Override
	public BaseUser currentUserInfo(String token) throws CustomerException {

		String id = JwtUtil.getUserId(token);

		BaseUser user = baseUserMapper.selectByPrimaryKey(Integer.parseInt(id));
		user.setPassword(null);

		return user;
	}

	@Override
	public PageInfo<BaseUser> pageQuery(int pageNum, int pageSize, String username, Integer role) {
		PageHelper.startPage(pageNum,pageSize,true);

		List<BaseUser> list = baseUserExtendMapper.pageQuery(username, role);

		return new PageInfo<>(list);
	}

	@Override
	public BaseUser findById(int id) {
		return baseUserMapper.selectByPrimaryKey(id);
	}


	@Transactional
	@Override
	public void updateUser(UpdateUserVM updateUserVM) throws CustomerException {
		/*
		能更新的字段：telephone realname gender dob email
		 */
		BaseUser baseUser = new BaseUser();
		//复制updateUserVM 中的数据到 baseUser
		BeanUtils.copyProperties(updateUserVM,baseUser);
		baseUserExtendMapper.updateUser(baseUser);

	}

	@Transactional
	@Override
	public void changeStatus(int id, int status) throws CustomerException {
		BaseUser userFromDB = baseUserMapper.selectByPrimaryKey(id);
		if (userFromDB == null) {
			throw new CustomerException(MessageStatus.USER_NOT_EXIST);
		}
		BaseUser u = new BaseUser();
		u.setUserId(id);
		u.setStatus(status);
		baseUserMapper.updateByPrimaryKeySelective(u);
	}

	@Transactional
	@Override
	public void deleteById(int id) throws CustomerException {
		BaseUser user = baseUserMapper.selectByPrimaryKey(id);
		if (user == null) {
			throw new CustomerException(MessageStatus.USER_NOT_EXIST);
		}
		if(user.getRole() == CmsConstants.SYSTEM_ROLE_ADMIN) {
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ADNIN_FORBID_DEL);
		}
		baseUserMapper.deleteByPrimaryKey(id);
	}

	@Transactional
	@Override
	public void updateUserIcon(int id, String userIcon) throws CustomerException {
		BaseUser user = baseUserMapper.selectByPrimaryKey(id);
		if (user == null) {
			throw new CustomerException(MessageStatus.USER_NOT_EXIST);
		}
		user.setIcon(userIcon);
		baseUserMapper.updateByPrimaryKey(user);
	}

	@Override
	public void register(BaseUser u) {
		// 初始化
		// 判断用户名是否被占用
//		BaseUserExample example = new BaseUserExample();
//		example.createCriteria().andUsernameEqualTo(u.getUsername());
//		List<BaseUser> list = baseUserMapper.selectByExample(example);
		List<BaseUser> list = new ArrayList<>();
		if (list.size() > 0) {
			throw new CustomerException("该用户已经被占用");
		}
		// 初始化
		u.setRegisterTime(new Date());
		u.setStatus(CmsConstants.USER_STATUS_NORMAL);
		u.setIcon("http://121.199.29.84:8888/group1/M00/00/12/rBD-SV-Td6-Aawn0AACq962TS9c719.jpg");
		baseUserMapper.insert(u);
	}
}
